# !/bin/sh

CONTAINER=${image}_${BUILD_NUMBER}

curl -X POST -F "buildNumber=${BUILD_NUMBER}" $callback_url
unzip -o answer.zip -d ./__answer_path
SCRIPT="evaluate-script-${BUILD_NUMBER}.sh"
IMAGE="$image"
cp ./__answer_path/script.sh $SCRIPT    # save script content to file
cat $SCRIPT
sudo docker run --tty --name $CONTAINER --detach $IMAGE /bin/sh -xc "tail -f /dev/null"         # just to hold container running

if [ -f "./build.gradle" ]; then  # modify dependency download url
rm -rf ./src/test
mv  __answer_path/src/test ./src
sed -i "/repositories {/a\    maven { url \"http:\/\/ec2-54-222-235-15.cn-north-1.compute.amazonaws.com.cn:8081\/repository\/maven-central\/\" }" ./build.gradle
fi

if [ -f "./gradle/wrapper/gradle-wrapper.properties" ]; then  # modify gradle download url
sed -i '/distributionUrl/d' ./gradle/wrapper/gradle-wrapper.properties
sed -i '$a\distributionUrl=https://s3.cn-north-1.amazonaws.com.cn/tws-development-files/gradle-4.4.1-bin.zip' ./gradle/wrapper/gradle-wrapper.properties
fi

sudo docker cp . $CONTAINER:/var/test-directory # copy directory

if [ -f "./gradlew" ]; then  # add exec to gradlew file
sudo docker exec -i $CONTAINER sh -c "cd /var/test-directory && chmod +x gradlew"
fi

sudo docker exec -i $CONTAINER sh -c "cd /var/test-directory && chmod +x $SCRIPT && ./$SCRIPT" > /tmp/result_detail_${BUILD_NUMBER} 2>&1  # evaluate in container
